package com.xzkydz.all_sensor.fragment;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.xzkydz.all_sensor.activity.MainActivity;
import com.xzkydz.all_sensor.allsensor.R;

import java.text.DecimalFormat;

/**
 * Created by Smilekun on 2016/8/22.
 */
public class AsFragment extends Fragment {

    private Button mButton3;
    private Button mButton4;
    private Button mButton2;
    private Button mButton;
    private float res_one;
    private float res_two;
    boolean run = true;
    private Thread thread;

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_as, container, false);
        final MainActivity mActivity = (MainActivity) getActivity();
        final DecimalFormat df2 = new DecimalFormat("####00.00");
        mButton = (Button) view.findViewById(R.id.btn_send_ask_);
        mButton2 = (Button) view.findViewById(R.id.btn_jisuan);
        mButton3 = (Button) view.findViewById(R.id.btn_ling_jiaozhun);
        mButton4 = (Button) view.findViewById(R.id.btn_sig_jiaozhun);
//        TextView addTextView = (TextView) view.findViewById(R.id.tv_add);
//        TextView jianTextView = (TextView) view.findViewById(R.id.tv_jian);
//        TextView initTextView = (TextView) view.findViewById(R.id.tv_init);
//        res_one = 0;        //加速度
//        res_two = 0;        //减速度
//
//        addTextView.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                res_one += 4.9f;
//            }
//        });
//
//        jianTextView.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                if (res_one > 0) res_one -= 4.9f;
//            }
//        });
//
//        initTextView.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                res_one = 0f;
//            }
//        });

//        if (thread == null) {
//            thread = new Thread(new Runnable() {
//                @Override
//                public void run() {
//                    while (run) {
//                        mActivity.runOnUiThread(new Runnable() {
//                            public void run() {
//                                float max = 0;
//                                float min = 0;
//                                if (res_one != 0) {
//                                    max = res_one + (float) (Math.random() * 0.6f - 0.3f) + (float) Math.random() * 0.01f;
//                                    min = res_one + (float) (Math.random() * 0.6f - 0.3f) + (float) Math.random() * 0.01f;
//                                }
//                                mActivity.ShowInf("最大加速度：  " + "   " + df2.format(max) + "   m/s2" + "\n" + "最小加速度：  " + "—" + df2.format(min) + "   m/s2" + "\n\n\n\n");
//                            }
//                        });
//                        try {
//                            Thread.sleep(1000);
//                        } catch (InterruptedException e) {
//                            // TODO Auto-generated catch block
//                            e.printStackTrace();
//                        }
//                    }
//                }
//            });
//            thread.start();
//        }

        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mActivity.sendPortData("4B590801050001A4010201AB");
                mActivity.ShowInf("发送请求：4B590801050001A4010201AB" + "\n");
                mActivity.list_acc.clear(); //清除list数据
            }
        });
        mButton2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mActivity.getMaxAndMin();
            }
        });

        mButton3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mActivity.sendPortData("4B590801050001A4010501AC");
                mActivity.ShowInf("0 点校准：4B590801050001A4010501AC" + "\n");
            }
        });

        mButton4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mActivity.sendPortData("4B590801050001A4010601AF");
                mActivity.ShowInf("4g 校准：4B590801050001A4010601AF" + "\n");
            }
        });
        return view;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        run = false;
    }
}
